package com.huluobo.a2201mvvm_imdemo.contact;

import androidx.lifecycle.MutableLiveData;

import com.huluobo.a2201mvvm_imdemo.mvvm.BaseRepo;
import com.hyphenate.EMValueCallBack;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by LC on 2024/4/1.
 */
public class ContactRepo extends BaseRepo {
    public void getAllContacts(MutableLiveData<List<ContactListItemBean>> success, MutableLiveData<String> failed) {
        emClient.contactManager().aysncGetAllContactsFromServer(new EMValueCallBack<List<String>>() {
            @Override
            public void onSuccess(List<String> value) {
                //把SDK返回的好友列表转换为自定义bean类的好友列表
                //创建自定义Bean的集合
                List<ContactListItemBean> contactListItemBeans = new ArrayList<>();
                //给返回的数据排序
                Collections.sort(value);

                //循环取出每一个value中的值(username)
                for (int i = 0; i < value.size(); i++) {
                    boolean showFirstLetter = false;
                    //第一个item肯定是要显示字符的,所以为true
                    //对比与上一个首字符不同,也为true
                    if (i == 0 || value.get(i).charAt(0) != value.get(i - 1).charAt(0)) {
                        showFirstLetter = true;
                    }

                    ContactListItemBean contactListItemBean = new ContactListItemBean(
                            value.get(i), //dd1 dd3
                            //Character.toUpperCase 转换一个char为大写
                            Character.toUpperCase(value.get(i).charAt(0)),
                            showFirstLetter
                    );
                    //把转换好的bean添加到list中
                    contactListItemBeans.add(contactListItemBean);
                }

                success.postValue(contactListItemBeans);
            }

            @Override
            public void onError(int error, String errorMsg) {
                failed.postValue(errorMsg);
            }
        });
    }


}
